import {Component} from '@angular/core';
import {AgRendererComponent} from '@ag-grid-community/angular';
import {ICellRendererParams} from '@ag-grid-community/core';
import { dateFormat, exists } from '../../../utils/utils';

@Component({
  selector: 'apes-time-range',
  template: `
    {{this.formatterValue(params.value)}}
   `,
  styles: []
})
export class ApesRenderTimeRangeComponent implements AgRendererComponent {
  value:any;
  params: ICellRendererParams;
  constructor() { }
  refresh(params: any): boolean {
    return false;
  }
  agInit(params: ICellRendererParams): void {
    this.params = params;
  }

  formatterValue(params){
    let value ="";
    if (! exists(params)) return "";
    params.forEach(item =>{
        if (item.courseDate){
          value = value + dateFormat("yyyy-MM-dd",new Date(item.courseDate)) +this.getWeek(new Date(item.courseDate)) +dateFormat("HH:mm",new Date(item.courseStateTime))+"至"+dateFormat("HH:mm",new Date(item.courseEndTime))+";\r"
        }
    }
    );
    return value;
  }

  getWeek(date){
    let weekDay = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    let myDate = new Date(date);
    return weekDay[myDate.getDay()]
  }
}
